package com.maxatime.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.maxatime.pojo.dto.StudentBasicInfoDTO;
import com.maxatime.pojo.entity.Areas;
import com.maxatime.pojo.entity.Cities;
import com.maxatime.pojo.entity.Provinces;
import com.maxatime.pojo.entity.StudentBasicInfo;
import com.maxatime.service.ProvincesS;
import com.maxatime.service.StudentBasicInfoService;
import com.maxatime.service.StudentRegisterService;
@RequestMapping("/Student")
@Controller
public class StudentController {
	
	// 日志
	Logger log = LogManager.getLogger();
	@Autowired
	StudentBasicInfoService StudentBasicInfoService1;
	@Autowired
	StudentRegisterService studentRegisterService1;
	@Autowired
	ProvincesS ProvincesS;
	
	@RequestMapping("/stumianpage")
	public String html() {
		
		return "Student/stumianpage";
	}
	/**
	 * 
	 * @Title: regiaterInsert   
	 * @Description: TODO(学生管理系统)   
	 * @param: @param val
	 * @param: @return      
	 * @return: Map<String,Object>      
	 * @throws   
	 * @author: ssb    
	 * @date:   2023年10月30日 下午2:03:56   
	 * @version V1.0
	 */
	//学生查询
	@ResponseBody
	@RequestMapping("/regiaterInsert")
	public Map<String, Object> regiaterInsert(@RequestBody String val) {
		Map<String, Object> m=new HashMap<String, Object>();
		JSONObject jsonObj =JSONObject.parseObject(val);
		String studentName=jsonObj.getString("studentName");
		String mobile=jsonObj.getString("mobile");
		String idNum=jsonObj.getString("idNum");
		String sex=jsonObj.getString("sex");
		String provinceid=jsonObj.getString("provinceid");
		String city=jsonObj.getString("city");
		String areas=jsonObj.getString("areas");
		//分页
		int pageNum = jsonObj.getIntValue("pageNum");
		int pageSize = jsonObj.getIntValue("pageSize");
		
		StudentBasicInfo StudentBasicInfo=new StudentBasicInfo();
		StudentBasicInfo.setStudentName(studentName);
		StudentBasicInfo.setMobile(mobile);
		StudentBasicInfo.setIdNum(idNum);
		StudentBasicInfo.setSex(sex);
		StudentBasicInfo.setProvincesId(provinceid);
		StudentBasicInfo.setCitiesId(city);
		StudentBasicInfo.setAreasId(areas);
		
		StudentBasicInfo.setPageNum(pageNum);
		StudentBasicInfo.setPageSize(pageSize);
		
		log.info("modifyData-req:::");
		Map<String, Object> map=StudentBasicInfoService1.selectByPrimaryKey(StudentBasicInfo);
		
		PageInfo<StudentBasicInfo> pageInfo = new PageInfo<>((List<com.maxatime.pojo.entity.StudentBasicInfo>) map.get("list"));  
        System.out.println("总记录数："+pageInfo.getTotal());  
        System.out.println("总页数："+pageInfo.getPages());  
        System.out.println("一页的大小："+pageInfo.getSize()); 
        
		
		m.put("data", map.get("list"));
		m.put("pageInfo", map.get("pageInfo"));
	    m.put("code", "0000");
	    m.put("msg", "交易成功");
		return m;
	}
	
	@RequestMapping("/regiaterInsert1")
	public String regiaterInsert1(Model model) {
		System.out.println("111");
		
		List<StudentBasicInfo> list=StudentBasicInfoService1.selectByPrimaryKey1();
		model.addAttribute("list1",list);
		/* System.out.println("查询结果："+list); */
		return "student/stumianpage";
	}
	//学生新增
	@ResponseBody
	@RequestMapping("/insert")
	public Map<String, Object> insert(@RequestBody String val) {
		log.info("insert::"+val);
		JSONObject jsonObject =JSONObject.parseObject(val);
		String studentName=jsonObject.getString("AddStudentName");
		String mobile=jsonObject.getString("AddMobile");
		String sex=jsonObject.getString("Addsex"); 
		String age=jsonObject.getString("AddAge");
		String idNum=jsonObject.getString("AddIdNum");
		String nativePlace=jsonObject.getString("AddNativePlace");
		String email=jsonObject.getString("AddEmail");
		
		String provinces = jsonObject.getString("provinces");//省
		String city = jsonObject.getString("city");//市
		String areas = jsonObject.getString("areas");//县
		
		System.out.println("接收："+studentName+mobile+age+idNum+nativePlace);
		StudentBasicInfoDTO studentBasicInfoDTO=new StudentBasicInfoDTO();
		studentBasicInfoDTO.setStudentName(studentName);
		studentBasicInfoDTO.setMobile(mobile);
		studentBasicInfoDTO.setSex(sex);
		studentBasicInfoDTO.setAge(Integer.parseInt(age));
		studentBasicInfoDTO.setIdNum(idNum);
		studentBasicInfoDTO.setNativePlace(nativePlace);
		studentBasicInfoDTO.setEmail(email);
		
		studentBasicInfoDTO.setProvincesId(provinces);//省
		studentBasicInfoDTO.setCitiesId(city);//市
		studentBasicInfoDTO.setAreasId(areas);//县
		
		StudentBasicInfoService1.insertSelective(studentBasicInfoDTO);
		Map<String, Object> m=new HashMap<String, Object>();
		return m;
	}
	
	@RequestMapping("/update1")
	public String update1(@RequestParam String studentId,Model model) {
		model.addAttribute("studentId", studentId);
		return "student/updatepage";
	}
	@RequestMapping("/update")
	public String update(
			@RequestParam String studentId,
			@RequestParam String studentName,
			@RequestParam String mobile,
			@RequestParam String sex,
			@RequestParam String age,
			@RequestParam String idNum,
			@RequestParam String nativePlace,
			@RequestParam String email) {
		System.out.println("接收1111："+studentName+mobile+sex+age+idNum+nativePlace);
		StudentBasicInfoDTO studentBasicInfoDTO=new StudentBasicInfoDTO();
		studentBasicInfoDTO.setStudentId(studentId);
		studentBasicInfoDTO.setStudentName(studentName);
		studentBasicInfoDTO.setMobile(mobile);
		studentBasicInfoDTO.setSex(sex);
		studentBasicInfoDTO.setAge(Integer.parseInt(age));
		studentBasicInfoDTO.setIdNum(idNum);
		studentBasicInfoDTO.setNativePlace(nativePlace);
		studentBasicInfoDTO.setEmail(email);
		StudentBasicInfoService1.updateByPrimaryKeySelective(studentBasicInfoDTO);
		
		return "student/stumianpage";
	}
	
	
	//省
	@RequestMapping("/findProvinces")
	@ResponseBody
	public Map<String,Object> findProvinces(@RequestBody  Provinces provinces){
		Map<String, Object> m = new HashMap<String, Object>();
		List<Provinces> list=ProvincesS.selectProvinces(provinces);
		m.put("data", list);
		m.put("code", "0000");
		m.put("msg", "success");
		return m;
	}
	//市
	@RequestMapping("/findCities")
	@ResponseBody
	public Map<String,Object> findCities(@RequestBody String val){
		JSONObject jsonObj=JSONObject.parseObject(val);
		String provinceid=jsonObj.getString("provinceId");
		
		Map<String,Object> m=new HashMap<String,Object>();
		Cities Ci=new Cities();
		
		
		Ci.setProvinceid(provinceid);
		List<Cities> list=ProvincesS.selectCities(Ci);
		m.put("data", list);
		m.put("code", "0000");
		m.put("msg", "success");
		return m;
	}
	
	//县
	@RequestMapping("/findAreas")
	@ResponseBody
	public Map<String,Object> findAreas(@RequestBody String val){
		JSONObject jsonObj=JSONObject.parseObject(val);
		String cityid=jsonObj.getString("cityid");
		Map<String,Object> m=new HashMap<String,Object>();
		Areas Areas=new Areas();
		Areas.setCityid(cityid);
		List<Areas> list=ProvincesS.selectAreas(Areas);
		
		m.put("data", list);
		m.put("code", "0000");
		m.put("msg", "success");
		return m;
	}
	
	//学生反显
	@RequestMapping("/findStu")
	@ResponseBody
	public Map<String,Object> findStu(@RequestBody String val){
		JSONObject jsonObj=JSONObject.parseObject(val);
		String studentId=jsonObj.getString("studentId");
		Map<String,Object> m=new HashMap<String,Object>();
		StudentBasicInfo StudentBasicInfo=new StudentBasicInfo();
		StudentBasicInfo.setStudentId(studentId);
		List<StudentBasicInfo> list=StudentBasicInfoService1.selectByPrimaryKey3(StudentBasicInfo);
		m.put("data", list);
		m.put("code", "0000");
		m.put("msg", "success");
		return m;
	}
	//学生修改
	@RequestMapping("/updateStudent")
	@ResponseBody
	public Map<String,Object> updateStudent(@RequestBody String val){
		JSONObject jsonObj=JSONObject.parseObject(val);
		
		String UpStudentName=jsonObj.getString("UpStudentName");
		String UpMobile=jsonObj.getString("UpMobile");
		String UpAge=jsonObj.getString("UpAge");
		String UpIdNum=jsonObj.getString("UpIdNum");
		String UpNativePlace=jsonObj.getString("UpNativePlace");
		String UpEmail=jsonObj.getString("UpEmail");
		String UpSex = jsonObj.getString("UpSex");
		
		String studentId=jsonObj.getString("studentId");
		Map<String,Object> m=new HashMap<String,Object>();
		StudentBasicInfoDTO StudentBasicInfoDTO=new StudentBasicInfoDTO();
		StudentBasicInfoDTO.setStudentName(UpStudentName);
		StudentBasicInfoDTO.setMobile(UpMobile);
		StudentBasicInfoDTO.setAge(Integer.parseInt(UpAge));
		StudentBasicInfoDTO.setIdNum(UpIdNum);
		StudentBasicInfoDTO.setNativePlace(UpNativePlace);
		StudentBasicInfoDTO.setEmail(UpEmail);
		StudentBasicInfoDTO.setSex(UpSex);
		StudentBasicInfoDTO.setStudentId(studentId);
		StudentBasicInfoService1.updateByPrimaryKeySelective(StudentBasicInfoDTO);
		
		m.put("code", "0000");
		m.put("msg", "success");
		return m;
	}
	//学生删除
	@RequestMapping("/deleteStudent")
	@ResponseBody
	public Map<String,Object> deleteStudent(@RequestBody String val){
		JSONObject jsonObj=JSONObject.parseObject(val);
		
		
		
		String studentId=jsonObj.getString("studentId");
		Map<String,Object> m=new HashMap<String,Object>();
		StudentBasicInfoDTO StudentBasicInfoDTO=new StudentBasicInfoDTO();
		
		StudentBasicInfoDTO.setStudentId(studentId);
		StudentBasicInfoService1.deleteByPrimaryKey(StudentBasicInfoDTO);
		
		m.put("code", "0000");
		m.put("msg", "success");
		return m;
	}
}
